package com.example.kotlin.service

import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.app.Service
import android.content.Context
import android.content.Intent
import android.graphics.BitmapFactory
import android.os.Binder
import android.os.Build
import android.os.IBinder
import android.util.Log
import androidx.core.app.NotificationCompat
import com.example.kotlin.R
import com.example.kotlin.activity.FirstActivity

class MyService : Service() {

    private val TAG = "MyService"

    private val mBinder = DownloadBinder()

    override fun onCreate() {
        super.onCreate()

        val manager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
            val channel=NotificationChannel("my_service","前台service通知",NotificationManager.IMPORTANCE_DEFAULT)
            manager.createNotificationChannel(channel)
        }
        val intent=Intent(this,FirstActivity::class.java)
        val pi=PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE)
        val notification=NotificationCompat.Builder(this,"my_service")
            .setContentTitle("这是通知标题")
            .setContentText("这是通知内容")
            .setSmallIcon(R.mipmap.smallheader)
            .setLargeIcon(BitmapFactory.decodeResource(resources,R.mipmap.largeheader))
            .setContentIntent(pi)
            .build()
        startForeground(1,notification)
    }

    override fun onBind(intent: Intent): IBinder {
        return mBinder
    }

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        return super.onStartCommand(intent, flags, startId)
    }

    override fun onDestroy() {
        super.onDestroy()
    }

    class DownloadBinder : Binder() {

        private val TAG = "MyService"

        fun startDownload() {
            Log.e(TAG, "startDownload: execute")
        }

        fun getProgess(): Int {
            Log.e(TAG, "getProgess: execute")
            Thread.sleep(1000)
            return 0
        }
    }

}